package com.dpj.service;

import com.dpj.mapper.BrandMapper;
import com.dpj.pojo.Brand;
import com.dpj.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.naming.InsufficientResourcesException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class BrandService {
    SqlSessionFactory sessionFactory = SqlSessionFactoryUtils.getSessionFactory();


    public  List<Brand> selectAll(){
        SqlSession sqlSession = sessionFactory.openSession(true);
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = mapper.selectAll();
        return brands;
    }

    public int add(Brand brand){
        SqlSession sqlSession = sessionFactory.openSession(true);
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        int i = mapper.addOneBrand(brand);
        return i;
    }

    public int delete(Integer id){
        SqlSession sqlSession = sessionFactory.openSession(true);
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        int i = mapper.deleteByID(id);
        return i;
    }

    public Brand selectById(Integer id){
        SqlSession sqlSession = sessionFactory.openSession(true);
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        Brand brand = mapper.selectByID(id);
        return brand;
    }


    public  int updateByID(Brand brand){
        SqlSession sqlSession = sessionFactory.openSession(true);
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        int i = mapper.updateById(brand);
        return i;

    }

}
